<template>
  <el-upload
    v-model:file-list="fileList"
    action="http://localhost:3001/admin/file/upload"
    name="img"
    list-type="picture-card"
    :on-preview="handlePictureCardPreview"
    :on-remove="handleRemove"
  >
    <el-icon><Plus /></el-icon>
  </el-upload>

  <el-dialog v-model="dialogVisible">
    <img w-full :src="dialogImageUrl" alt="Preview Image" />
  </el-dialog>
  <div>
    <el-button type="primary" @click="onSubmit">提交</el-button>
    <el-button @click="clearPir">清空</el-button>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { lunboAddRequest } from '@/api/lunbo'
let fileList = ref([])
let dialogVisible = ref(false)
let dialogImageUrl = ref('')
let imgData = ref({})

const handleRemove = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles)
}

const handlePictureCardPreview = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url
  dialogVisible.value = true
}

//点击提交
function onSubmit() {
  imgData.value = fileList.value.map((v) => {
    return v.response.fileUrl
  })
  let alt = 'img'
  //把图片地址传入接口
  lunboAddRequest({alt,img}).then((r) => {
    console.log(r.data)
  })
  console.log(fileList.value)
}
//清空图片
const clearPir = () => {
  fileList.value = []
}
</script>

<style></style>
